// pages/pay/index.js
var productService = require("../../service/product/index");

//index.js
Page({

  data: {
    swiperItems: [],
    productList:[],
    indexCate: [],
    youlikes: [],
    indexCateAndProducts: [],
    top: 0
  },

  onLoad: function () {
    // 加载轮播
    wx.request({
      url : 'http://grace.hcoder.net/api/swipers',
      success: (res) => { this.setData({ swiperItems: res.data.data });}
    });
    // 加载首页推荐图标
    wx.request({
      url : 'http://grace.hcoder.net/api/cate/indexCate',
      success : (res) => { this.setData({ indexCate: res.data.data });}
    });
    // 猜你喜欢
    this.youlike();
    // 加载首页推荐分类及产品
    wx.request({
      url : 'http://grace.hcoder.net/api/products/indexTj',
      success: (res) => {this.setData({ indexCateAndProducts: res.data.data});}
    });

    this.findProducts();
  },


  findProducts(){
    productService.loadProducts().then(res=>{
      this.setData({productList:res.productList});
    })
  },

  // 搜索相关函数
  inputting: function (e) { console.log(e); },

  confirm: function (e) { console.log(e); },
  // 猜你喜欢
  youlike: function () {
    wx.request({
      url : 'http://grace.hcoder.net/api/products/youlike',
      success : (res) => { this.setData({ youlikes: res.data.data }); }
    });
  },

  // 打开商品详情
  openProductInfo: function (e) {
    var {index} = e.currentTarget.dataset;
    var pid = this.data.productList[index].id;
    console.log("当前查询的产品是：",pid)
    wx.navigateTo({ url: '../productInfo/productInfo?opid='+pid+"&cid=1111"});
  }

})
